<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Rails::Generators::Migration</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            Rails::Generators::Migration 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/railties/lib/rails/generators/migration_rb.html">railties/lib/rails/generators/migration.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>Holds common methods for migrations. It assumes that migrations has the
[0-9]*_name format and can be used by another frameworks (like Sequel) just
by implementing the next migration version method.</p>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">MODULE</span>
          <a href="Migration/ClassMethods.html">Rails::Generators::Migration::ClassMethods</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>M</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Migration.html#method-i-migration_template">migration_template</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    
      <!-- Section attributes -->
      <div class="sectiontitle">Attributes</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class='attr-rw'>
              [R]
            </td>
            <td class='attr-name'>migration_class_name</td>
            <td class='attr-desc'></td>
          </tr>
        
          <tr valign='top'>
            <td class='attr-rw'>
              [R]
            </td>
            <td class='attr-name'>migration_file_name</td>
            <td class='attr-desc'></td>
          </tr>
        
          <tr valign='top'>
            <td class='attr-rw'>
              [R]
            </td>
            <td class='attr-name'>migration_number</td>
            <td class='attr-desc'></td>
          </tr>
        
      </table>
    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-migration_template">
            
              <b>migration_template</b>(source, destination=nil, config={})
            
            <a href="Migration.html#method-i-migration_template" name="method-i-migration_template" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Creates a migration template at the given destination. The difference to
the default template method is that the migration version is appended to
the destination file name.</p>

<p>The migration version, migration file name, migration class name are
available as instance variables in the template to be rendered.</p>

<pre>migration_template &quot;migration.rb&quot;, &quot;db/migrate/add_foo_to_bar.rb&quot;</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-migration_template_source')" id="l_method-i-migration_template_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b4c99ea0a4d7e5223fa1b21cdb9301f9d090dbfe/railties/lib/rails/generators/migration.rb#L41" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-migration_template_source" class="dyn-source">
                <pre><span class="ruby-comment"># File railties/lib/rails/generators/migration.rb, line 41</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">migration_template</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">destination</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">config</span>={})
  <span class="ruby-identifier">destination</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span>(<span class="ruby-identifier">destination</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">source</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">destination_root</span>)

  <span class="ruby-identifier">migration_dir</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">dirname</span>(<span class="ruby-identifier">destination</span>)
  <span class="ruby-ivar">@migration_number</span>     = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">next_migration_number</span>(<span class="ruby-identifier">migration_dir</span>)
  <span class="ruby-ivar">@migration_file_name</span>  = <span class="ruby-constant">File</span>.<span class="ruby-identifier">basename</span>(<span class="ruby-identifier">destination</span>).<span class="ruby-identifier">sub</span>(<span class="ruby-regexp">/\.rb$/</span>, <span class="ruby-string">''</span>)
  <span class="ruby-ivar">@migration_class_name</span> = <span class="ruby-ivar">@migration_file_name</span>.<span class="ruby-identifier">camelize</span>

  <span class="ruby-identifier">destination</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">migration_exists?</span>(<span class="ruby-identifier">migration_dir</span>, <span class="ruby-ivar">@migration_file_name</span>)

  <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span>(<span class="ruby-identifier">destination</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:skip</span>]) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">behavior</span> <span class="ruby-operator">==</span> <span class="ruby-value">:invoke</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">destination</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">force?</span>
      <span class="ruby-identifier">remove_file</span>(<span class="ruby-identifier">destination</span>)
    <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">destination</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">Error</span>, <span class="ruby-node">&quot;Another migration is already named #{@migration_file_name}: #{destination}. Use --force to remove the old migration file and replace it.&quot;</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">destination</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-identifier">migration_dir</span>, <span class="ruby-node">&quot;#{@migration_number}_#{@migration_file_name}.rb&quot;</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">template</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">destination</span>, <span class="ruby-identifier">config</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    